package no.ntnu.fp.server;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

public abstract class Console {
	//UGLY workaround debug code because the KTN code cannot disable console output
	//we reroute that output to a file called serverlog.txt so we can use the actual
	//console for ourselves

	static File logFile = new File("Log/serverlog.txt");
	static PrintStream ps;	
	static PrintStream dps = System.out;
	
	public static void printConsole(String text){
		printRaw(text, false);
	}

	public static void printError(String text){
		printRaw(text, true);
	}

	private static void printRaw(String text, boolean error){
		if( ps == null ){
			try {
				new File("Log").mkdir();
				logFile.createNewFile();
				ps = new PrintStream(logFile);
			} catch (IOException e) {
				e.printStackTrace();
			}			
		}

		System.setOut(dps);
		if(!error) System.out.println(text); else System.err.println(text);
	    System.setOut(ps);		
	}
}
